# coding=utf-8
import urllib.request
import urllib.response
import re


# 1.TypeError: cannot use a string pattern on a bytes-like object
# 这种情况解决方法就是加上html=html.decode('utf-8')#python3这句代码
#
# 2.AttributeError: module 'urllib' has no attribute 'urlopen'
# 这种情况的解决办法就是将urllib改成urllib.request就行了

# 可以设置超时时间
def getHtml(url):
    try:
        page = urllib.request.urlopen(url, timeout=0.1)
        html = page.read()
    except Exception as err:
        print("错误记录", err)
    return html


# def getImg(html):
#     reg = r'src="(.+?\.jpg)" pic_ext'
#     imgre = re.compile(reg)
#     imglist = re.findall(imgre, html)
#     return imglist

def getImg(html):
    reg = r'src="(.+?\.jpg)" pic_ext'
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html)
    x = 0
    for imgurl in imglist:
        urllib.request.urlretrieve(imgurl, 'D:\imge\%s.jpg' % x)
        x += 1


def getimgs(html):
    try:
        reg = r'src="(.*?\.jpg)"'
        img = re.compile(reg)
        html = html.decode('utf-8')  # python3
        imglist = re.findall(img, html)
        x = 0
    except UnboundLocalError as err:
        print("错误路劲", err)
    for imgurl in imglist:
        try:
            urllib.request.urlretrieve(imgurl, 'D:\imge\%s.jpg' % x)
        except ValueError as err:
            print("错误路劲", err)
        x = x + 1


html = getHtml("http://news.ifeng.com/a/20161115/50258273_0.shtml")
print(html)
print(getimgs(html))
